import operate from '@dfeidao/atom-nodejs/db/operate';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';

interface Message {
	hidden_source: string;
	hidden: {
		hidden_source_feature_no: string;
		hidden_source_feature_name: string;
		hidden_source_feature_code: string;
		control_data: string;
	};
	filter: {
		companyid: string;
		companyname: string;
		factory_no: string;
		factory_name: string;
		status_manage: string;
	};
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}
/* 表名	manage_data
标题	管理数据
字段名称	字段标题	字段类型	字段长度	是否为空	是否主键
_id	id	string	50	ⅹ	√
productid	产品ID	string	50	√	ⅹ
hidden_source	隐患源编号	string	32	√	ⅹ
change_field	更改字段	string	0	√	ⅹ
change_before	更改前	string	32	√	ⅹ
change_after	更改后	string	32	√	ⅹ
change_personnel	更改人	string	50	√	ⅹ
change_date	更改时间	timestamp	50	√	ⅹ
hidden_source_feature_no	隐患源特性编号	string	50	√	ⅹ
 */
export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:f:\实习\kh\src\wyf-hManager\zj-000007\s003,action_id:' + action_id);


	// 插入隐患源特性数据
	const { exec, insert } = operate(action_id, session_id, systemid);
	insert('hazard_source_feature', {
		hidden_source: message.hidden_source,
		companyid: message.filter.companyid,
		companyname: message.filter.companyname,
		factory_no: message.filter.factory_no,
		factory_name: message.filter.factory_name,
		hidden_source_feature_no: message.hidden.hidden_source_feature_no,
		hidden_source_feature_name: message.hidden.hidden_source_feature_name,
		hidden_source_feature_code: message.hidden.hidden_source_feature_code,
		status_manage: message.filter.status_manage,
		control_data: message.hidden.control_data,
		productid
	});

	// 插入管理数据表数据
	const now_date = new Date().getTime();// 获取当前时间

	insert('manage_data', { hidden_source: message.hidden_source, hidden_source_feature_no: message.hidden.hidden_source_feature_no, change_field: "隐患源特性名称", change_before: "", change_after: message.hidden.hidden_source_feature_name, change_personnel: "weiwei", change_date: now_date, productid });

	insert('manage_data', { hidden_source: message.hidden_source, hidden_source_feature_no: message.hidden.hidden_source_feature_no, change_field: "控制数据", change_before: "", change_after: message.hidden.control_data, change_personnel: "weiwei", change_date: now_date, productid });

	insert('manage_data', { hidden_source: message.hidden_source, hidden_source_feature_no: message.hidden.hidden_source_feature_no, change_field: "公司名称", change_before: "", change_after: message.filter.companyname, change_personnel: "weiwei", change_date: now_date, productid });

	insert('manage_data', { hidden_source: message.hidden_source, hidden_source_feature_no: message.hidden.hidden_source_feature_no, change_field: "工厂名称", change_before: "", change_after: message.filter.factory_name, change_personnel: "weiwei", change_date: now_date, productid });

	insert('manage_data', { hidden_source: message.hidden_source, hidden_source_feature_no: message.hidden.hidden_source_feature_no, change_field: "状态管理", change_before: "", change_after: message.filter.status_manage, change_personnel: "weiwei", change_date: now_date, productid });

	const r = await exec();

	log('Service end path:f:\实习\kh\src\wyf-hManager\zj-000007\s003,action_id:' + action_id);
	return {
		data: r
	};
}
